// packageF/others/customerCenter/component/userDetail/my_customer/my_customer.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    keywork: {
      type: null
    },
    memberId: {
      type: null
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    strUrl: 'plugin.wechat-chat-sidebar.frontend.controller.customer.customer.get-member-list',
    workConfig: {
      client: 'work',
      type: 17
    },
    agent_data: [],
    level_tab: [],
    my_referral: {},
    customerLevel: 'level1',
    json: {
      relationLevel: 1,
      keyword: '',
      page: 1,
    },
    loading: false,
    finished: false,
  },
  lifetimes: {
    attached() {
      this.initData();
      this.getData();
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    search(e){
      this.setData({
        'json.keyword':e.detail,
        'json.page':1,
        loading:false,
        finished:false,
      })
      this.getData();
    },
    loadMore() {
      this.getData();
    },
    initData() {
      this.data.json = {
        relationLevel: 1,
        keyword: '',
        page: 1,
      };
      this.setData({
        customerLevel: 'level1',
        finished: false,
        loading: false,
      })
    },
    changeLevel(e) {
      console.log(e);
      let name = e.detail.name;
      let index = 0;
      switch (name) {
        case "level1":
          index = 1;
          break;
        case "level2":
          index = 2;
          break;
        default:
          break;
      }
      this.setData({
        'json.page': 1,
        'json.keyword': '',
        'json.relationLevel': index,
        finished: false,
        loading: false,
      })
      this.getData();
    },
    getData() {
      if (this.finished && this.loading) {
        return;
      }
      this.loading = true;
      let json = {};
      if (this.data.keywork == 'customer') {
        this.data.strUrl = 'plugin.customer-center.frontend.controllers.customer.get-member-list';
        this.data.json.member_id = this.data.memberId
        json = {
          ...this.data.json
        };
      } else {
        this.data.strUrl = 'plugin.wechat-chat-sidebar.frontend.controller.customer.customer.get-member-list';
        json = {
          ...this.data.workConfig,
          ...this.data.json
        };
      }
      let urlStr = app.getNetAddresss(this.data.strUrl);
      app._postNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data
          if (res.result == 1) {
            // 客户中心
            this.setData({
              my_referral: res.data.my_referral,
              level_tab: res.data.my_agent.json,
            })
            let agentData = res.data.agent_data.json;
            if (this.data.json.page >= agentData.last_page || agentData.data.length < agentData.per_page) {
              this.setData({
                finished: true
              })
            }
            if (res.data.agent_data && res.data.agent_data.json) {
              let list = res.data.agent_data.json.data;
              this.setData({
                agent_data: this.data.json.page > 1 ? this.data.agent_data.concat(list) : list
              })
            }
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  }
})